<template>
  <div class="classroom-list">
    <el-card>
      <div slot="header">
        <span>我的班级</span>
      </div>

      <el-table
        :data="classroomList"
        style="width: 100%"
        v-loading="loading">
        <el-table-column
          prop="name"
          label="班级名称"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="code"
          label="班级代码"
          min-width="120">
        </el-table-column>
        <el-table-column
          prop="studentCount"
          label="学生人数"
          width="100">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          min-width="180">
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
          fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleViewDetails(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          layout="total, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ClassroomList',
  data() {
    return {
      classroomList: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchClassroomList()
  },
  methods: {
    async fetchClassroomList() {
      this.loading = true
      try {
        const response = await this.$http.get('/api/teacher/classroom/list', {
          params: {
            current: this.currentPage,
            size: this.pageSize
          }
        })

        if (response.code === 1) {
          this.classroomList = response.data.records
          this.total = response.data.total
        } else {
          this.$message.error(response.msg || '获取班级列表失败')
        }
      } catch (error) {
        console.error('获取班级列表失败:', error)
        this.$message.error('获取班级列表失败')
      } finally {
        this.loading = false
      }
    },
    handleViewDetails(row) {
      // TODO: 实现查看班级详情功能
      console.log('查看班级详情:', row)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchClassroomList()
    }
  }
}
</script>

<style scoped>
.classroom-list {
  padding: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
